package com.demo.blog;

import com.demo.common.GlobalConstant;
import com.demo.model.Blog;
import com.demo.service.BlogService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.spring.Inject;

/**
 * BlogController
 * 所有 sql 写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
public class BlogController extends Controller {

	private Logger logger = Logger.getLogger(getClass());

	@Inject.BY_TYPE
	private BlogService blogService;
	
	public void index() {
		Page<Blog> page = blogService.pageQueryBlog(getParaToInt(0, 1), GlobalConstant.PAGE_SIZE);
		setAttr("a", 1);
		setAttr("blogPage", page);
		render("blog.html");
	}
	
	public void add() {
	}
	
	@Before(BlogValidator.class)
	public void save() {
		getModel(Blog.class).save();
		redirect("/blog");
	}
	
	public void edit() {
		setAttr("blog", Blog.dao.findById(getParaToInt()));
	}
	
	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		Blog.dao.deleteById(getParaToInt());
		redirect("/blog");
	}
}


